package com.andnnl.mq;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.ReUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.regex.Pattern;

public class MQInfo {
    private static Logger logger = LoggerFactory.getLogger(MQInfo.class);
    private static HashMap<String, String> httpHeaderMap = new HashMap();
    private static HashMap<String, String> httpHeaderMap2 = new HashMap();
    private static final String ua = "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0";

    public MQInfo() {

//    httpHeaderMap.put("Host", Const.MqInfoServerIp1);
        httpHeaderMap.put("User-Agent", ua);
        httpHeaderMap.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        httpHeaderMap.put("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
        httpHeaderMap.put("Accept-Encoding", "gzip, deflate");
        //v52xbjpjwwpzlvqycediw2ny
        //httpHeaderMap.put("Referer", "http://192.168.10.58:8161/admin/");
//    httpHeaderMap.put("Cookie", "JSESSIONID=3l1vofvjd0yp85tzye2tw3se");
//    httpHeaderMap.put("Authorization", "Basic YWRtaW46anlAODgyNTA4NjA=");
        httpHeaderMap.put("Authorization", "Basic YWRtaW46YWRtaW4=");
        httpHeaderMap.put("Connection", "keep-alive");
        httpHeaderMap.put("Cache-Control", "max-age=0");


    }


    public void testGet() {

        String url = "http://192.168.4.240:8168/admin";
        HttpResponse resp = HttpRequest.get(url)
                .setConnectionTimeout(3000)
                .setReadTimeout(3000)
                .setFollowRedirects(true)
                .header("Authorization", "Basic YWRtaW46YWRtaW4=")
                .execute();
        String result = resp.body();
//      logger.info("rs:" + result);
        logger.info("cookies:" + resp.getCookies());
        String url2 = "http://192.168.4.240:8168/admin/queues.jsp";
        HttpResponse resp2 = HttpRequest.get(url2)
                .setConnectionTimeout(3000)
                .setReadTimeout(3000)
                .setFollowRedirects(true)
                .header("Authorization", "Basic YWRtaW46YWRtaW4=")
                .header("cookies", resp.getCookieStr())
                .execute();
        logger.info("cookies:" + resp2.getCookies());
//        logger.info("rs:" + resp2.body());
        Pattern regex = Pattern.compile("<a href=\"(purgeDestination.action.+?=sms11026&JMSDestinationType=queue.+?)\">Purge</a>", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.MULTILINE);
        String pp = ReUtil.getGroup1(regex, resp2.body());
//        String pp = ReUtil.getGroup1("<a href=\"(purgeDestination.action.+?=sms11026&JMSDestinationType=queue.+?)\">Purge</a>", resp2.body());
        System.out.println(pp);
//        String url3 = "http://192.168.4.240:8168/admin/purgeDestination.action?JMSDestination=sms11026&JMSDestinationType=queue&secret=a0c1f2c6-e0b2-4f8a-a01c-2b7f35ea7924";
        String url3 = "http://192.168.4.240:8168/admin/" + pp;
        System.out.println(url3);
        String cookieStr = resp2.getCookieStr();
        HttpResponse resp3 = HttpRequest.get(url3)
                .setConnectionTimeout(3000)
                .setReadTimeout(3000)
                .setFollowRedirects(true)
                .header("Authorization", "Basic YWRtaW46YWRtaW4=")
                .header("cookies", cookieStr)
                .execute();
        logger.info("cookies:" + resp3.getCookies());
        logger.info("status:" + resp3.getStatus());

        pp = ReUtil.getGroup1(regex, resp3.body());
        String url4 = "http://192.168.4.240:8168/admin/" + pp.replaceAll("sms11026", "sms11026-f");
        System.out.println(url4);
        HttpResponse resp4 = HttpRequest.get(url4)
                .setConnectionTimeout(3000)
                .setReadTimeout(3000)
                .setFollowRedirects(true)
                .header("Authorization", "Basic YWRtaW46YWRtaW4=")
                .header("cookies", cookieStr)
                .execute();
        logger.info("cookies:" + resp4.getCookies());
        logger.info("status:" + resp4.getStatus());

    }

    public static void main(String[] args) {
        MQInfo info = new MQInfo();
//        info.testGet();
      System.out.println(Base64.encode("admin:admin"));
    }
}
